﻿using PearTunes.WEB.Controllers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting.Web;
using System.Web.Mvc;
using System.Collections.Generic;
using System.Linq;
using PearTunes.Model;

namespace PearTunes.UnitTest
{
    
    
    /// <summary>
    ///This is a test class for HomeControllerTest and is intended
    ///to contain all HomeControllerTest Unit Tests
    ///</summary>
    [TestClass()]
    public class HomeControllerTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        ///// <summary>
        /////A test for AddMoney
        /////</summary>
        //[TestMethod()]
        ////[HostType("ASP.NET")]
        ////[AspNetDevelopmentServerHost("D:\\cucc\\000_Progi\\Pear\\PearTunes\\PearTunes\\PearTunes.WEB", "/")]
        ////[UrlToTest("http://localhost:2913/Home/Money")]
        //public void AddMoneyTest()
        //{
        //    DAL.PearTunesEntities context = new DAL.PearTunesEntities();
        //    context.Users.Add(new User() { Name = "TestDummy", Money = 0 });
        //    context.SaveChanges();
        //    WEB.Models.UserModel m = new WEB.Models.UserModel();
        //    HomeController target = new HomeController();
        //    target.AddMoney();
        //    int expected = 1000;
        //    int actual = (new DAL.PearTunesEntities()).Users.Where(u => u.Name == "TestDummy").First().Money;
        //    Assert.AreEqual(expected, actual);
        //}



        ///// <summary>
        /////A test for DeleteTrack
        /////</summary>
        //[TestMethod()]
        ////[HostType("ASP.NET")]
        ////[AspNetDevelopmentServerHost("D:\\cucc\\000_Progi\\Pear\\PearTunes\\PearTunes\\PearTunes.WEB", "/")]
        ////[UrlToTest("http://localhost:2913/")]
        //public void DeleteTrackTest()
        //{
        //    DAL.PearTunesEntities context = new DAL.PearTunesEntities();
        //    context.Tracks.Add(new Track() { Title = "TestDummy" });
        //    context.SaveChanges();
        //    HomeController target = new HomeController();
        //    target.DeleteTrack("1");
        //    int expected = 0;
        //    int actual = (new DAL.PearTunesEntities()).Tracks.Count();
        //    Assert.AreEqual(expected, actual);
        //}

        /// <summary>
        ///A test for DeleteUser
        ///</summary>
        [TestMethod()]
        //[HostType("ASP.NET")]
        //[AspNetDevelopmentServerHost("D:\\cucc\\000_Progi\\Pear\\PearTunes\\PearTunes\\PearTunes.WEB", "/")]
        //[UrlToTest("http://localhost:2913/")]
        public void DeleteUserTest()
        {
            DAL.PearTunesEntities context = new DAL.PearTunesEntities();
            context.Users.Add(new User() { Name = "Controller_DeleteUserTest" });
            context.SaveChanges();
            HomeController target = new HomeController();
            target.DeleteUser((new DAL.PearTunesEntities()).Users.Where(u => u.Name == "Controller_DeleteUserTest").First().UserId.ToString());
            int expected = 0;
            int actual = (new DAL.PearTunesEntities()).Tracks.Where(tr => tr.Title == "Controller_DeleteUserTest").Count();
            Assert.AreEqual(expected, actual);
        }



        /// <summary>
        ///A test for ListAlbums
        ///<para>Tests if the number of returned albums are euqal to the number of albums in the database.</para>
        ///</summary>
        [TestMethod()]
        //[HostType("ASP.NET")]
        //[AspNetDevelopmentServerHost("D:\\cucc\\000_Progi\\Pear\\PearTunes\\PearTunes\\PearTunes.WEB", "/")]
        //[UrlToTest("http://localhost:2913/")]
        public void ListAlbumsTest()
        {
            DAL.PearTunesEntities context = new DAL.PearTunesEntities();
            int expected = 0;
            if (context.Albums.Count() > 0)
                expected = context.Albums.Count();
            else
            {
                context.Albums.Add(new Album() { Title = "Controller_ListAlbumsTest1" });
                context.Albums.Add(new Album() { Title = "Controller_ListAlbumsTest2" });
                context.Albums.Add(new Album() { Title = "Controller_ListAlbumsTest3" });
                context.SaveChanges();
                expected = 3;
            }
            HomeController target = new HomeController();
            int actual = ((target.ListAlbums() as ViewResult).ViewData.Model as IEnumerable<PearTunes.Model.Album>).Count();
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for ListTracks
        ///<para>Tests if the number of returned tracks are euqal to the number of tracks in the database.</para>
        ///</summary>
        [TestMethod()]
        //[HostType("ASP.NET")]
        //[AspNetDevelopmentServerHost("D:\\cucc\\000_Progi\\Pear\\PearTunes\\PearTunes\\PearTunes.WEB", "/")]
        //[UrlToTest("http://localhost:2913/")]
        public void ListTracksTest()
        {
            DAL.PearTunesEntities context = new DAL.PearTunesEntities();
            int expected = 0;
            if (context.Tracks.Count() > 0)
                expected = context.Tracks.Count();
            else
            {
                context.Tracks.Add(new Track() { Title = "Controller_ListTracksTest1" });
                context.Tracks.Add(new Track() { Title = "Controller_ListTracksTest2" });
                context.Tracks.Add(new Track() { Title = "Controller_ListTracksTest3" });
                context.SaveChanges();
                expected = 3;
            }
            HomeController target = new HomeController();
            int actual = ((target.ListTracks() as ViewResult).ViewData.Model as IEnumerable<PearTunes.Model.Track>).Count();
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for ManageUsers
        ///<para>Tests if the number of returned users are euqal to the number of registered users.</para>
        ///</summary>
        [TestMethod()]
        //[HostType("ASP.NET")]
        //[AspNetDevelopmentServerHost("D:\\cucc\\000_Progi\\Pear\\PearTunes\\PearTunes\\PearTunes.WEB", "/")]
        //[UrlToTest("http://localhost:2913/")]
        public void ManageUsersTest()
        {
            DAL.PearTunesEntities context = new DAL.PearTunesEntities();
            int expected = 0;
            if (context.Users.Count() > 0)
                expected = context.Users.Count();
            else
            {
                context.Users.Add(new User() { Name = "Controller_ManageUsersTest1" });
                context.Users.Add(new User() { Name = "Controller_ManageUsersTest2" });
                context.Users.Add(new User() { Name = "Controller_ManageUsersTest3" });
                context.SaveChanges();
                expected = 3;
            }
            HomeController target = new HomeController();
            int actual = ((target.ManageUsers() as ViewResult).ViewData.Model as IEnumerable<PearTunes.Model.User>).Count();
            Assert.AreEqual(expected, actual);
        }

        ///// <summary>
        /////A test for Money
        /////</summary>
        //[TestMethod()]
        ////[HostType("ASP.NET")]
        ////[AspNetDevelopmentServerHost("D:\\cucc\\000_Progi\\Pear\\PearTunes\\PearTunes\\PearTunes.WEB", "/")]
        ////[UrlToTest("http://localhost:2913/")]
        //public void MoneyTest()
        //{
        //    HomeController target = new HomeController();

        //    string actual = (target.Money() as ViewResult).View.ToString();
        //    Assert.AreEqual("Money", actual);

        //}

        ///// <summary>
        /////A test for NewTrack
        /////</summary>
        //// TODO: Ensure that the UrlToTest attribute specifies a URL to an ASP.NET page (for example,
        //// http://.../Default.aspx). This is necessary for the unit test to be executed on the web server,
        //// whether you are testing a page, web service, or a WCF service.
        //[TestMethod()]
        //[HostType("ASP.NET")]
        //[AspNetDevelopmentServerHost("D:\\cucc\\000_Progi\\Pear\\PearTunes\\PearTunes\\PearTunes.WEB", "/")]
        //[UrlToTest("http://localhost:2913/")]
        //public void NewTrackTest()
        //{
        //    HomeController target = new HomeController(); // TODO: Initialize to an appropriate value
        //    ActionResult expected = null; // TODO: Initialize to an appropriate value
        //    ActionResult actual;
        //    actual = target.NewTrack();
        //    Assert.AreEqual(expected, actual);
        //    Assert.Inconclusive("Verify the correctness of this test method.");
        //}

    }
}
